
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.analysis.reaction_calculator &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.reaction_calculator</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.analysis.reaction_calculator</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides classes that define a chemical reaction.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MSONable</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.composition</span> <span class="kn">import</span> <span class="n">Composition</span>
<span class="kn">from</span> <span class="nn">pymatgen.entries.computed_entries</span> <span class="kn">import</span> <span class="n">ComputedEntry</span>
<span class="kn">from</span> <span class="nn">monty.json</span> <span class="kn">import</span> <span class="n">MontyDecoder</span>
<span class="kn">from</span> <span class="nn">monty.fractions</span> <span class="kn">import</span> <span class="n">gcd_float</span>

<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">combinations</span><span class="p">,</span> <span class="n">chain</span>


<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong, Anubhav Jain&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2011, The Materials Project&quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;2.0&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Shyue Ping Ong&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;shyuep@gmail.com&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;Jul 11 2012&quot;</span>


<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="BalancedReaction"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction">[docs]</a><span class="k">class</span> <span class="nc">BalancedReaction</span><span class="p">(</span><span class="n">MSONable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An object representing a complete chemical reaction.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c1"># Tolerance for determining if a particular component fraction is &gt; 0.</span>
    <span class="n">TOLERANCE</span> <span class="o">=</span> <span class="mf">1e-6</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reactants_coeffs</span><span class="p">,</span> <span class="n">products_coeffs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reactants and products to be specified as dict of {Composition: coeff}.</span>

<span class="sd">        Args:</span>
<span class="sd">            reactants_coeffs ({Composition: float}): Reactants as dict of</span>
<span class="sd">                {Composition: amt}.</span>
<span class="sd">            products_coeffs ({Composition: float}): Products as dict of</span>
<span class="sd">                {Composition: amt}.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># sum reactants and products</span>
        <span class="n">all_reactants</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span>
            <span class="p">[</span><span class="n">k</span> <span class="o">*</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">reactants_coeffs</span><span class="o">.</span><span class="n">items</span><span class="p">()],</span> <span class="n">Composition</span><span class="p">({})</span>
        <span class="p">)</span>
        <span class="n">all_products</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">k</span> <span class="o">*</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">products_coeffs</span><span class="o">.</span><span class="n">items</span><span class="p">()],</span> <span class="n">Composition</span><span class="p">({}))</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">all_reactants</span><span class="o">.</span><span class="n">almost_equals</span><span class="p">(</span><span class="n">all_products</span><span class="p">,</span> <span class="n">rtol</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">atol</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">):</span>
            <span class="k">raise</span> <span class="n">ReactionError</span><span class="p">(</span><span class="s2">&quot;Reaction is unbalanced!&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_els</span> <span class="o">=</span> <span class="n">all_reactants</span><span class="o">.</span><span class="n">elements</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">reactants_coeffs</span> <span class="o">=</span> <span class="n">reactants_coeffs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">products_coeffs</span> <span class="o">=</span> <span class="n">products_coeffs</span>

        <span class="c1"># calculate net reaction coefficients</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_els</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">set</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">reactants_coeffs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">products_coeffs</span><span class="o">.</span><span class="n">keys</span><span class="p">())):</span>
            <span class="n">coeff</span> <span class="o">=</span> <span class="n">products_coeffs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-</span> <span class="n">reactants_coeffs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">coeff</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">coeff</span><span class="p">)</span>

<div class="viewcode-block" id="BalancedReaction.calculate_energy"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.calculate_energy">[docs]</a>    <span class="k">def</span> <span class="nf">calculate_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">energies</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculates the energy of the reaction.</span>

<span class="sd">        Args:</span>
<span class="sd">            energies ({Composition: float}): Energy for each composition.</span>
<span class="sd">                E.g ., {comp1: energy1, comp2: energy2}.</span>

<span class="sd">        Returns:</span>
<span class="sd">            reaction energy as a float.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">sum</span><span class="p">([</span><span class="n">amt</span> <span class="o">*</span> <span class="n">energies</span><span class="p">[</span><span class="n">c</span><span class="p">]</span> <span class="k">for</span> <span class="n">amt</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">)])</span></div>

<div class="viewcode-block" id="BalancedReaction.normalize_to"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.normalize_to">[docs]</a>    <span class="k">def</span> <span class="nf">normalize_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">,</span> <span class="n">factor</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalizes the reaction to one of the compositions.</span>
<span class="sd">        By default, normalizes such that the composition given has a</span>
<span class="sd">        coefficient of 1. Another factor can be specified.</span>

<span class="sd">        Args:</span>
<span class="sd">            comp (Composition): Composition to normalize to</span>
<span class="sd">            factor (float): Factor to normalize to. Defaults to 1.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">scale_factor</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="mi">1</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">comp</span><span class="p">)]</span> <span class="o">*</span> <span class="n">factor</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="o">*</span> <span class="n">scale_factor</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">]</span></div>

<div class="viewcode-block" id="BalancedReaction.normalize_to_element"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.normalize_to_element">[docs]</a>    <span class="k">def</span> <span class="nf">normalize_to_element</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">factor</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalizes the reaction to one of the elements.</span>
<span class="sd">        By default, normalizes such that the amount of the element is 1.</span>
<span class="sd">        Another factor can be specified.</span>

<span class="sd">        Args:</span>
<span class="sd">            element (Element/Specie): Element to normalize to.</span>
<span class="sd">            factor (float): Factor to normalize to. Defaults to 1.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">all_comp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span>
        <span class="n">coeffs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span>
        <span class="n">current_el_amount</span> <span class="o">=</span> <span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="n">all_comp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">element</span><span class="p">]</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">coeffs</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                                  <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">all_comp</span><span class="p">))])</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
        <span class="n">scale_factor</span> <span class="o">=</span> <span class="n">factor</span> <span class="o">/</span> <span class="n">current_el_amount</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span> <span class="o">*</span> <span class="n">scale_factor</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coeffs</span><span class="p">]</span></div>

<div class="viewcode-block" id="BalancedReaction.get_el_amount"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.get_el_amount">[docs]</a>    <span class="k">def</span> <span class="nf">get_el_amount</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the amount of the element in the reaction.</span>

<span class="sd">        Args:</span>
<span class="sd">            element (Element/Specie): Element in the reaction</span>

<span class="sd">        Returns:</span>
<span class="sd">            Amount of that element in the reaction.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">sum</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">element</span><span class="p">]</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">))])</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">elements</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of elements in the reaction</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_els</span><span class="p">[:]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">coeffs</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Final coefficients of the calculated reaction</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[:]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">all_comp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of all compositions in the reaction.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reactants</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of reactants</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">))</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">products</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of products</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">))</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>

<div class="viewcode-block" id="BalancedReaction.get_coeff"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.get_coeff">[docs]</a>    <span class="k">def</span> <span class="nf">get_coeff</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns coefficient for a particular composition</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">comp</span><span class="p">)]</span></div>

<div class="viewcode-block" id="BalancedReaction.normalized_repr_and_factor"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.normalized_repr_and_factor">[docs]</a>    <span class="k">def</span> <span class="nf">normalized_repr_and_factor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalized representation for a reaction</span>
<span class="sd">        For example, ``4 Li + 2 O -&gt; 2Li2O`` becomes ``2 Li + O -&gt; Li2O``</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str_from_comp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">normalized_repr</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A normalized representation of the reaction. All factors are converted</span>
<span class="sd">        to lowest common factors.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">normalized_repr_and_factor</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">other</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">comp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">:</span>
            <span class="n">coeff2</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">get_coeff</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span> <span class="k">if</span> <span class="n">comp</span> <span class="ow">in</span> <span class="n">other</span><span class="o">.</span><span class="n">_all_comp</span> <span class="k">else</span> <span class="mi">0</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_coeff</span><span class="p">(</span><span class="n">comp</span><span class="p">)</span> <span class="o">-</span> <span class="n">coeff2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="fm">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">7</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_str_from_formulas</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">coeffs</span><span class="p">,</span> <span class="n">formulas</span><span class="p">):</span>
        <span class="n">reactant_str</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">product_str</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">amt</span><span class="p">,</span> <span class="n">formula</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">coeffs</span><span class="p">,</span> <span class="n">formulas</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">amt</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">cls</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="n">reactant_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">formula</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">abs</span><span class="p">(</span><span class="n">amt</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">cls</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="n">product_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">formula</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">amt</span> <span class="o">&lt;</span> <span class="o">-</span><span class="bp">cls</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="n">reactant_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{:.4g}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">-</span><span class="n">amt</span><span class="p">,</span> <span class="n">formula</span><span class="p">))</span>
            <span class="k">elif</span> <span class="n">amt</span> <span class="o">&gt;</span> <span class="bp">cls</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">:</span>
                <span class="n">product_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{:.4g}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">amt</span><span class="p">,</span> <span class="n">formula</span><span class="p">))</span>

        <span class="k">return</span> <span class="s2">&quot; + &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">reactant_str</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; -&gt; &quot;</span> <span class="o">+</span> <span class="s2">&quot; + &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">product_str</span><span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_str_from_comp</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">coeffs</span><span class="p">,</span> <span class="n">compositions</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">r_coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">coeffs</span><span class="p">))</span>
        <span class="n">r_formulas</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">amt</span><span class="p">,</span> <span class="n">comp</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">coeffs</span><span class="p">,</span> <span class="n">compositions</span><span class="p">)):</span>
            <span class="n">formula</span><span class="p">,</span> <span class="n">factor</span> <span class="o">=</span> <span class="n">comp</span><span class="o">.</span><span class="n">get_reduced_formula_and_factor</span><span class="p">()</span>
            <span class="n">r_coeffs</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">amt</span> <span class="o">*</span> <span class="n">factor</span>
            <span class="n">r_formulas</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">formula</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reduce</span><span class="p">:</span>
            <span class="n">factor</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="n">gcd_float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">r_coeffs</span><span class="p">))</span>
            <span class="n">r_coeffs</span> <span class="o">*=</span> <span class="n">factor</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">factor</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="o">.</span><span class="n">_str_from_formulas</span><span class="p">(</span><span class="n">r_coeffs</span><span class="p">,</span> <span class="n">r_formulas</span><span class="p">),</span> <span class="n">factor</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_str_from_comp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

    <span class="fm">__repr__</span> <span class="o">=</span> <span class="fm">__str__</span>

<div class="viewcode-block" id="BalancedReaction.as_entry"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.as_entry">[docs]</a>    <span class="k">def</span> <span class="nf">as_entry</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">energies</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a ComputedEntry representation of the reaction.</span>
<span class="sd">        :return:</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">relevant_comp</span> <span class="o">=</span> <span class="p">[</span><span class="n">comp</span> <span class="o">*</span> <span class="nb">abs</span><span class="p">(</span><span class="n">coeff</span><span class="p">)</span> <span class="k">for</span> <span class="n">coeff</span><span class="p">,</span> <span class="n">comp</span>
                         <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">)]</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">relevant_comp</span><span class="p">,</span> <span class="n">Composition</span><span class="p">())</span>
        <span class="n">entry</span> <span class="o">=</span> <span class="n">ComputedEntry</span><span class="p">(</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">comp</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">calculate_energy</span><span class="p">(</span><span class="n">energies</span><span class="p">))</span>
        <span class="n">entry</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__str__</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">entry</span></div>

<div class="viewcode-block" id="BalancedReaction.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            A dictionary representation of BalancedReaction.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
            <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s2">&quot;reactants&quot;</span><span class="p">:</span> <span class="p">{</span>
                <span class="nb">str</span><span class="p">(</span><span class="n">comp</span><span class="p">):</span> <span class="n">coeff</span> <span class="k">for</span> <span class="n">comp</span><span class="p">,</span> <span class="n">coeff</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactants_coeffs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="p">},</span>
            <span class="s2">&quot;products&quot;</span><span class="p">:</span> <span class="p">{</span>
                <span class="nb">str</span><span class="p">(</span><span class="n">comp</span><span class="p">):</span> <span class="n">coeff</span> <span class="k">for</span> <span class="n">comp</span><span class="p">,</span> <span class="n">coeff</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">products_coeffs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
            <span class="p">},</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="BalancedReaction.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): from as_dict()</span>

<span class="sd">        Returns:</span>
<span class="sd">            A BalancedReaction object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reactants</span> <span class="o">=</span> <span class="p">{</span><span class="n">Composition</span><span class="p">(</span><span class="n">comp</span><span class="p">):</span> <span class="n">coeff</span> <span class="k">for</span> <span class="n">comp</span><span class="p">,</span> <span class="n">coeff</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;reactants&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="n">products</span> <span class="o">=</span> <span class="p">{</span><span class="n">Composition</span><span class="p">(</span><span class="n">comp</span><span class="p">):</span> <span class="n">coeff</span> <span class="k">for</span> <span class="n">comp</span><span class="p">,</span> <span class="n">coeff</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;products&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">reactants</span><span class="p">,</span> <span class="n">products</span><span class="p">)</span></div>

<div class="viewcode-block" id="BalancedReaction.from_string"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.BalancedReaction.from_string">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="n">rxn_string</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates a balanced reaction from a string. The reaction must</span>
<span class="sd">        already be balanced.</span>

<span class="sd">        Args:</span>
<span class="sd">            rxn_string:</span>
<span class="sd">                The reaction string. For example, &quot;4 Li + O2-&gt; 2Li2O&quot;</span>

<span class="sd">        Returns:</span>
<span class="sd">            BalancedReaction</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">rct_str</span><span class="p">,</span> <span class="n">prod_str</span> <span class="o">=</span> <span class="n">rxn_string</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&gt;&quot;</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">get_comp_amt</span><span class="p">(</span><span class="n">comp_str</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">{</span>
                <span class="n">Composition</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)):</span> <span class="nb">float</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">1</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span>
                    <span class="sa">r</span><span class="s2">&quot;([\d\.]*(?:[eE]-?[\d\.]+)?)\s*([A-Z][\w\.\(\)]*)&quot;</span><span class="p">,</span> <span class="n">comp_str</span>
                <span class="p">)</span>
            <span class="p">}</span>

        <span class="k">return</span> <span class="n">BalancedReaction</span><span class="p">(</span><span class="n">get_comp_amt</span><span class="p">(</span><span class="n">rct_str</span><span class="p">),</span> <span class="n">get_comp_amt</span><span class="p">(</span><span class="n">prod_str</span><span class="p">))</span></div></div>


<div class="viewcode-block" id="Reaction"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.Reaction">[docs]</a><span class="k">class</span> <span class="nc">Reaction</span><span class="p">(</span><span class="n">BalancedReaction</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A more flexible class representing a Reaction. The reaction amounts will</span>
<span class="sd">    be automatically balanced. Reactants and products can swap sides so that</span>
<span class="sd">    all coefficients are positive, however this class will find the solution</span>
<span class="sd">    with the minimum number of swaps and coefficients of 0. Normalizes so that</span>
<span class="sd">    the *FIRST* product (or products, if underdetermined) has a coefficient of one.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reactants</span><span class="p">,</span> <span class="n">products</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Reactants and products to be specified as list of</span>
<span class="sd">        pymatgen.core.structure.Composition.  e.g., [comp1, comp2]</span>

<span class="sd">        Args:</span>
<span class="sd">            reactants ([Composition]): List of reactants.</span>
<span class="sd">            products ([Composition]): List of products.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_input_reactants</span> <span class="o">=</span> <span class="n">reactants</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_input_products</span> <span class="o">=</span> <span class="n">products</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span> <span class="o">=</span> <span class="n">reactants</span> <span class="o">+</span> <span class="n">products</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_num_comp</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">all_comp</span><span class="p">)</span>

        <span class="n">all_elems</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">({</span><span class="n">elem</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">})</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_num_elems</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">all_elems</span><span class="p">)</span>

        <span class="n">comp_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="n">c</span><span class="p">[</span><span class="n">el</span><span class="p">]</span> <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">all_elems</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">])</span><span class="o">.</span><span class="n">T</span>

        <span class="n">rank</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">matrix_rank</span><span class="p">(</span><span class="n">comp_matrix</span><span class="p">)</span>
        <span class="n">diff</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_num_comp</span> <span class="o">-</span> <span class="n">rank</span>
        <span class="n">num_constraints</span> <span class="o">=</span> <span class="n">diff</span> <span class="k">if</span> <span class="n">diff</span> <span class="o">&gt;=</span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">1</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_lowest_num_errors</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span>  <span class="c1"># an error = a component changing sides or disappearing</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_coeffs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_balance_coeffs</span><span class="p">(</span><span class="n">comp_matrix</span><span class="p">,</span> <span class="n">num_constraints</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_els</span> <span class="o">=</span> <span class="n">all_elems</span>

    <span class="k">def</span> <span class="nf">_balance_coeffs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">comp_matrix</span><span class="p">,</span> <span class="n">max_num_constraints</span><span class="p">):</span>
        <span class="n">first_product_idx</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_input_reactants</span><span class="p">)</span>

        <span class="c1"># start with simplest product constraints, work towards most complex reactant constraints</span>
        <span class="n">product_constraints</span> <span class="o">=</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="n">combinations</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">first_product_idx</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_num_comp</span><span class="p">),</span> <span class="n">n_constr</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">n_constr</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_num_constraints</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
            <span class="p">]</span>
        <span class="p">)</span>
        <span class="n">reactant_constraints</span> <span class="o">=</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="n">combinations</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">first_product_idx</span><span class="p">),</span> <span class="n">n_constr</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">n_constr</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_num_constraints</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
            <span class="p">]</span>
        <span class="p">)</span>
        <span class="n">best_soln</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">balanced</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">for</span> <span class="n">constraints</span> <span class="ow">in</span> <span class="n">chain</span><span class="p">(</span><span class="n">product_constraints</span><span class="p">,</span> <span class="n">reactant_constraints</span><span class="p">):</span>
            <span class="n">n_constr</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">constraints</span><span class="p">)</span>

            <span class="n">comp_and_constraints</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
                <span class="n">comp_matrix</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">n_constr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_num_comp</span><span class="p">)),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span>
            <span class="p">)</span>
            <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_num_elems</span> <span class="o">+</span> <span class="n">n_constr</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
            <span class="n">b</span><span class="p">[</span><span class="o">-</span><span class="n">n_constr</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="nb">min</span><span class="p">(</span><span class="n">constraints</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="n">first_product_idx</span> <span class="k">else</span> <span class="o">-</span><span class="mi">1</span>

            <span class="k">for</span> <span class="n">num</span><span class="p">,</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">constraints</span><span class="p">):</span>
                <span class="n">comp_and_constraints</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_num_elems</span> <span class="o">+</span> <span class="n">num</span><span class="p">,</span> <span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="c1"># arbitrarily fix coeff to 1</span>

            <span class="n">coeffs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">pinv</span><span class="p">(</span><span class="n">comp_and_constraints</span><span class="p">),</span> <span class="n">b</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">allclose</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">comp_matrix</span><span class="p">,</span> <span class="n">coeffs</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_num_elems</span><span class="p">,</span> <span class="mi">1</span><span class="p">))):</span>
                <span class="n">balanced</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">expected_signs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_input_reactants</span><span class="p">)</span> <span class="o">+</span>
                                          <span class="p">[</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_input_products</span><span class="p">))</span>
                <span class="n">num_errors</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">multiply</span><span class="p">(</span><span class="n">expected_signs</span><span class="p">,</span> <span class="n">coeffs</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">TOLERANCE</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">num_errors</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_lowest_num_errors</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">coeffs</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">num_errors</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lowest_num_errors</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_lowest_num_errors</span> <span class="o">=</span> <span class="n">num_errors</span>
                    <span class="n">best_soln</span> <span class="o">=</span> <span class="n">coeffs</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">balanced</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ReactionError</span><span class="p">(</span><span class="s2">&quot;Reaction cannot be balanced.&quot;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">best_soln</span><span class="p">)</span>

<div class="viewcode-block" id="Reaction.copy"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.Reaction.copy">[docs]</a>    <span class="k">def</span> <span class="nf">copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a copy of the Reaction object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">Reaction</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reactants</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">products</span><span class="p">)</span></div>

<div class="viewcode-block" id="Reaction.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.Reaction.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            A dictionary representation of Reaction.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
            <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s2">&quot;reactants&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">comp</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">comp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_input_reactants</span><span class="p">],</span>
            <span class="s2">&quot;products&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">comp</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">comp</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_input_products</span><span class="p">],</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="Reaction.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.Reaction.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): from as_dict()</span>

<span class="sd">        Returns:</span>
<span class="sd">            A Reaction object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reactants</span> <span class="o">=</span> <span class="p">[</span><span class="n">Composition</span><span class="p">(</span><span class="n">sym_amt</span><span class="p">)</span> <span class="k">for</span> <span class="n">sym_amt</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;reactants&quot;</span><span class="p">]]</span>
        <span class="n">products</span> <span class="o">=</span> <span class="p">[</span><span class="n">Composition</span><span class="p">(</span><span class="n">sym_amt</span><span class="p">)</span> <span class="k">for</span> <span class="n">sym_amt</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;products&quot;</span><span class="p">]]</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">reactants</span><span class="p">,</span> <span class="n">products</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="ReactionError"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.ReactionError">[docs]</a><span class="k">class</span> <span class="nc">ReactionError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exception class for Reactions. Allows more information in exception</span>
<span class="sd">    messages to cover situations not covered by standard exception classes.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a ReactionError.</span>

<span class="sd">        Args:</span>
<span class="sd">            msg (str): More information about the ReactionError.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">msg</span> <span class="o">=</span> <span class="n">msg</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">msg</span></div>


<div class="viewcode-block" id="ComputedReaction"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.ComputedReaction">[docs]</a><span class="k">class</span> <span class="nc">ComputedReaction</span><span class="p">(</span><span class="n">Reaction</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convenience class to generate a reaction from ComputedEntry objects, with</span>
<span class="sd">    some additional attributes, such as a reaction energy based on computed</span>
<span class="sd">    energies.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reactant_entries</span><span class="p">,</span> <span class="n">product_entries</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            reactant_entries ([ComputedEntry]): List of reactant_entries.</span>
<span class="sd">            product_entries ([ComputedEntry]): List of product_entries.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reactant_entries</span> <span class="o">=</span> <span class="n">reactant_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_product_entries</span> <span class="o">=</span> <span class="n">product_entries</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_all_entries</span> <span class="o">=</span> <span class="n">reactant_entries</span> <span class="o">+</span> <span class="n">product_entries</span>
        <span class="n">reactant_comp</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span>
            <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_reduced_composition_and_factor</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">reactant_entries</span><span class="p">])</span>
        <span class="n">product_comp</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span>
            <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_reduced_composition_and_factor</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">product_entries</span><span class="p">])</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">reactant_comp</span><span class="p">),</span> <span class="nb">list</span><span class="p">(</span><span class="n">product_comp</span><span class="p">))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">all_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Equivalent of all_comp but returns entries, in the same order as the</span>
<span class="sd">        coefficients.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">entries</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_comp</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_entries</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span> <span class="o">==</span> <span class="n">c</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">:</span>
                    <span class="n">entries</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
                    <span class="k">break</span>
        <span class="k">return</span> <span class="n">entries</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">calculated_reaction_energy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns (float):</span>
<span class="sd">            The calculated reaction energy.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">calc_energies</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reactant_entries</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_product_entries</span><span class="p">:</span>
            <span class="p">(</span><span class="n">comp</span><span class="p">,</span> <span class="n">factor</span><span class="p">)</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">get_reduced_composition_and_factor</span><span class="p">()</span>
            <span class="n">calc_energies</span><span class="p">[</span><span class="n">comp</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span>
                <span class="n">calc_energies</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">comp</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">)),</span> <span class="n">entry</span><span class="o">.</span><span class="n">energy</span> <span class="o">/</span> <span class="n">factor</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">calculate_energy</span><span class="p">(</span><span class="n">calc_energies</span><span class="p">)</span>

<div class="viewcode-block" id="ComputedReaction.as_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.ComputedReaction.as_dict">[docs]</a>    <span class="k">def</span> <span class="nf">as_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns:</span>
<span class="sd">            A dictionary representation of ComputedReaction.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;@module&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span>
            <span class="s2">&quot;@class&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span>
            <span class="s2">&quot;reactants&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reactant_entries</span><span class="p">],</span>
            <span class="s2">&quot;products&quot;</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_product_entries</span><span class="p">],</span>
        <span class="p">}</span></div>

<div class="viewcode-block" id="ComputedReaction.from_dict"><a class="viewcode-back" href="../../../pymatgen.analysis.reaction_calculator.html#pymatgen.analysis.reaction_calculator.ComputedReaction.from_dict">[docs]</a>    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">from_dict</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">            d (dict): from as_dict()</span>

<span class="sd">        Returns:</span>
<span class="sd">            A ComputedReaction object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">dec</span> <span class="o">=</span> <span class="n">MontyDecoder</span><span class="p">()</span>
        <span class="n">reactants</span> <span class="o">=</span> <span class="p">[</span><span class="n">dec</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;reactants&quot;</span><span class="p">]]</span>
        <span class="n">products</span> <span class="o">=</span> <span class="p">[</span><span class="n">dec</span><span class="o">.</span><span class="n">process_decoded</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">d</span><span class="p">[</span><span class="s2">&quot;products&quot;</span><span class="p">]]</span>
        <span class="k">return</span> <span class="bp">cls</span><span class="p">(</span><span class="n">reactants</span><span class="p">,</span> <span class="n">products</span><span class="p">)</span></div></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.analysis.reaction_calculator</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>